package com.example.aicooking.controller;

import com.example.aicooking.entity.dto.UserUpdateDTO;
import com.example.aicooking.entity.vo.UserBasicVO;
import com.example.aicooking.entity.vo.UserDetailVO;
import com.example.aicooking.util.BusinessException;
import com.example.aicooking.util.Result;
import com.example.aicooking.entity.User;
import com.example.aicooking.service.UserService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result<String> register(@RequestBody User user) {
        try {
            // 生成随机 userID
            int randomUserId = generateRandomUserId();
            // 检查 ID 是否已存在
            while (userService.checkUserIdExists(randomUserId)) {
                randomUserId = generateRandomUserId();
            }
            user.setUserId((long) randomUserId);

            userService.register(user);
            return Result.success("注册成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/login")
    public Result<User> login(@RequestBody Map<String, String> loginForm) {
        try {
            User user = userService.login(loginForm.get("username"), loginForm.get("password"));

            return Result.success(user);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }


    @GetMapping("/basic/{userId}")
    public Result<UserBasicVO> getBasicInfo(@PathVariable Integer userId) {
        try {
            UserBasicVO basicInfo = userService.getBasicInfo(userId);
            return Result.success(basicInfo);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            log.error("获取用户基本信息失败", e);
            return Result.error("系统错误");
        }
    }

    @GetMapping("/detail/{userId}")
    public Result<UserDetailVO> getDetailInfo(@PathVariable Long userId) {
        try {
            UserDetailVO detailInfo = userService.getDetailInfo(userId);
            return Result.success(detailInfo);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            log.error("获取用户详细信息失败", e);
            return Result.error("系统错误");
        }
    }

    @PutMapping("/update")
    public Result<Boolean> updateUserInfo(@RequestBody @Validated UserUpdateDTO updateDTO) {
        try {
            boolean success = userService.updateUserInfo(updateDTO);
            return success ? Result.success(true) : Result.error("更新失败");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            log.error("更新用户信息失败", e);
            return Result.error("系统错误");
        }
    }

    // 添加生成随机 userID 的方法
    private int generateRandomUserId() {
        Random random = new Random();
        // 生成6位数的随机数 (100000-999999)
        return random.nextInt(900000) + 100000;
    }
}